﻿using System;
using IteraProvisioning.Core;
using Microsoft.SharePoint.Client;
using System.Net;

namespace ProvisioningLibrary.Handlers
{
    public class Handler
    {
        protected ClientContext GetContext(string url)
        {
            if (!String.IsNullOrEmpty(url))
            {
                var siteUri = new Uri(url);
                string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);
                var token = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;
                return TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), token);
            }
            return null;
        }
    }
}